package com.kmxd.ams.controller.system;

import com.kmxd.ams.client.system.ISysCommonSvc;
import com.kmxd.ams.client.system.dto.SwitchFondsDTO;
import com.kmxd.ams.client.system.dto.SwitchUnitDTO;
import com.kmxd.ams.client.system.dto.SysInitDTO;
import com.kmxd.ams.core.common.bo.LoginUser;
import com.kmxd.ams.core.common.constant.ErrorCode;
import com.kmxd.ams.core.common.enums.LogLevelEnum;
import com.kmxd.ams.core.common.enums.LogTypeEnum;
import com.kmxd.ams.core.common.enums.OssEnum;
import com.kmxd.ams.core.exception.BizTipException;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.oss.bo.OssFile;
import com.kmxd.ams.core.oss.template.OssTemplate;
import com.kmxd.ams.core.result.R;
import com.kmxd.ams.core.result.ResultCode;
import com.kmxd.ams.infra.arrange.base.BaseBO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 通用common 前端控制器
 *
 * @author admin
 */
@RestController
@RequestMapping("/system/common")
@AllArgsConstructor
@Tag(name = "系统公共接口")
public class SysCommonCtl {

  private OssTemplate ossTemplate;
  private ISysCommonSvc sysCommonSvc;

  /** 系统初始化 */
  @Operation(summary = "初始化系统")
  @PostMapping("/initSystem")
  @AutoLog(
      module = "通用模块",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.HIGH_RISK,
      title = "初始化系统")
  public R<Void> initSystem(@Validated @RequestBody SysInitDTO req) {
    sysCommonSvc.init(req);
    return R.success();
  }

  /** 是否已经初始化系统 */
  @Operation(summary = "是否已经初始化系统")
  @PostMapping("/initSystemStatus")
  @AutoLog(
      module = "通用模块",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.HIGH_RISK,
      title = "是否已经初始化系统")
  public R<Boolean> initSystemStatus() {
    return R.success(sysCommonSvc.initSystemStatus());
  }

  @SneakyThrows
  @PostMapping("/upload")
  @AutoLog(
      module = "通用模块",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.HIGH_RISK,
      title = "上传文件")
  public R<OssFile> upload(MultipartFile file, @RequestParam String path) {
    if (Objects.isNull(file)) {
      throw BizTipException.instance(ErrorCode.EXCEPTION, ResultCode.PARAM_MISS.getMessage());
    }
    OssFile ossFile = ossTemplate.upload(file, OssEnum.attachment, path);
    return R.success(ossFile);
  }

  @SneakyThrows
  @PostMapping("/delete")
  @AutoLog(
      module = "通用模块",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.HIGH_RISK,
      title = "文件删除")
  public R<Boolean> delete(@RequestParam String path) {
    if (Objects.isNull(path)) {
      throw BizTipException.instance(ErrorCode.EXCEPTION, ResultCode.PARAM_MISS.getMessage());
    }
    return R.success(ossTemplate.delete(path));
  }

  @Operation(summary = "切换单位")
  @PostMapping("/switchUnit")
  @AutoLog(module = "通用模块", type = LogTypeEnum.LOGIN, level = LogLevelEnum.MEDIUM, title = "切换单位")
  public R<LoginUser> switchUnit(@Validated @RequestBody SwitchUnitDTO req) {
    return R.success(sysCommonSvc.switchUnit(req));
  }

  @Operation(summary = "切换全宗")
  @PostMapping("/switchFonds")
  @AutoLog(module = "通用模块", type = LogTypeEnum.LOGIN, level = LogLevelEnum.MEDIUM, title = "切换全宗")
  public R<LoginUser> switchFonds(@Validated @RequestBody SwitchFondsDTO req) {
    return R.success(sysCommonSvc.switchFonds(req));
  }

  @Operation(summary = "返回本单位")
  @PostMapping("/backCrop")
  @AutoLog(module = "通用模块", type = LogTypeEnum.LOGIN, level = LogLevelEnum.MEDIUM, title = "返回本单位")
  public R<LoginUser> backCrop(@RequestBody BaseBO req) {
    return R.success(sysCommonSvc.backCrop(req));
  }
}
